package com.css.bjdt_core.explain.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.css.bjdt_core.executive.report.ReportCache;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.explain.entity.RptCellExplain;
import com.css.bjdt_core.explain.entity.RptExplainAreaTemporary;
import com.css.bjdt_core.explain.entity.RptTemplateExplain;
import com.css.bjdt_core.explain.entity.VRptTemplateExplain;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.PageQuerys;

@Service
public class ReportTemplateExplainService {

	@Autowired
	private Dao dao;

	@Autowired
	private RptExplainAreaTemporaryService rptExplainAreaTemporaryService;

	@Autowired
	private RptCellExplainService rptCellExplainService;

	private ReportCache cache = ReportCache.getInstance();

	private final static String CACHE_NAME = ReportTemplateExplainService.class.getSimpleName();

	/**
	 * 分页查询统计年度报表
	 * @param cnd
	 * @return
	 */
	public Map queryPage(Condition cnd, int pageNum, int pageSize) {
		Map result = PageQuerys.ligerGridQuery(dao, VRptTemplateExplain.class, cnd, pageNum, pageSize);
		return result;
	}

	public RptTemplateExplain saveAExplain(RptTemplateExplain record) {
		if (StringUtils.isEmpty(record.getId())) {
			record.setId(CommonUtil.getUUID());
		}
		return dao.insert(record);
	}

	public boolean delete(String explainId) {
		if (StringUtils.isBlank(explainId)) {
			return false;
		}
		dao.clear(RptTemplateExplain.class, Cnd.where("ID", "=", explainId));
		return true;
	}

	public void updateRptTemplateExplain(RptTemplateExplain record) {
		dao.updateIgnoreNull(record);
	}

	public RptTemplateExplain getARptTemplateExplain(String explainId) {
		return dao.fetch(RptTemplateExplain.class, Cnd.where("ID", "=", explainId));
	}

	public List<RptTemplateExplain> getAllExplainByTemplateAndType(String templateId, String type) {
		List<RptTemplateExplain> res = new ArrayList<>();
		if(type != null) {
			for (RptTemplateExplain explain : getAllRptTemplateExplainByTemplateId(templateId)) {
				if (type.equals(explain.getExplainType())) {
					res.add(explain);
				}
			}
		}
		return res;
	}

	public void saveRptExplainAreaCategoryCell(List<RptCellExplain> rptCellExplain,
			List<RptExplainAreaTemporary> rptExplainAreaTemporaryList, String explainId) {
		//先删除报表区域
		rptExplainAreaTemporaryService.deleteAllRptExplainAreaTemporaryByExplainId(explainId);
		//再删除该报表下的所有单元格
		rptCellExplainService.deleteAllCellByExplainId(explainId);
		//保存该张报表报表的所有数据区域
		for (RptExplainAreaTemporary rptExplainAreaTemporary : rptExplainAreaTemporaryList) {
			rptExplainAreaTemporaryService.saveRptExplainAreaTemporary(rptExplainAreaTemporary);
		}
		//保存所有单元格
		rptCellExplainService.batchInsert(rptCellExplain);
	}

	public List<RptTemplateExplain> getAllRptTemplateExplainByTemplateId(String templateId) {
		List<RptTemplateExplain> res = (List<RptTemplateExplain>) cache.get(CACHE_NAME, templateId);
		if(res == null) {
			res = dao.query(RptTemplateExplain.class, Cnd.where("TEMPLATE_ID", "=", templateId).desc("ORDER_ID"));
			if(res == null) {
				res = new ArrayList<>();
			}
			cache.put(CACHE_NAME, templateId, res);
		}
		return res;
	}

	public List<RptTemplateExplain> getAllRptTemplateExplainByTemplateIdNeedValidation(String templateId) {
		List<RptTemplateExplain> res = new ArrayList<>();
		for(RptTemplateExplain explain : getAllRptTemplateExplainByTemplateId(templateId)) {
			if("2".equals(explain.getIsValidation())) {
				res.add(explain);
			}
		}
		return res;
	}

	public List<RptTemplateExplain> getAllRptTemplateExplainByAnnualId(String annualId) {
		return dao.query(RptTemplateExplain.class, Cnd.where("ANNUAL_ID", "=", annualId));
	}

	public RptTemplateExplain getAnRptTemplateExplain(String templateId, String templateCol, String templateRow) {
		if(templateCol == null || templateRow == null) {
			return null;
		}
		for(RptTemplateExplain explain : getAllRptTemplateExplainByTemplateId(templateId)) {
			if(templateCol.equals(explain.getTemplateCol()) && templateRow.equals(explain.getTemplateRow())) {
				return explain;
			}
		}
		return null;
	}
}
